<template>
  <div id="register" style="width: 100%; height: 100vh; overflow: hidden">
    <div style="width: 400px; margin: 100px auto">
      <div align="center">
        <img alt="Vue logo" src="../assets/img1.png">
      </div>


<!--      private String phone;-->
      <div style="color: #cccccc; font-size: 30px; text-align: center; padding: 30px 0">欢迎注册</div>
      <el-form style="background-color: #cccccc79 ;padding: 50px" ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="用户名" prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-radio-group v-model="form.sex" class="ml-4">

            <el-radio label="男" size="large">男</el-radio>
            <el-radio label="女" size="large">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="age">
          <el-input placeholder="年龄" prefix-icon="el-icon-user-solid" v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item prop="jobTitle">
          <el-select v-model="form.jobTitle"  placeholder="职位" >
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input placeholder="手机号码" prefix-icon="el-icon-user-solid" v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请设置密码" prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input placeholder="请确认密码" prefix-icon="el-icon-lock" v-model="form.confirm" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Register",
  data() {
    return {
      form: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        sex:[
          {required: true,message: '请选择性别',trigger: 'change'}
        ],
        jobTitle:[
          {required:true,message:'请选择职位',trigger:'blur'}
        ],
        age:[
          {required: true, message: '请输入年龄', trigger: 'blur'},
        ],
        phone:[
          {required: true, message: '请输入手机号码', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        confirm: [
          {required: true, message: '请确认密码', trigger: 'blur'},
        ],
      },
      options : [
        {
          value: 'Option1',
          label: 'Option1',
        },
        {
          value: 'Option2',
          label: 'Option2',
        },
        {
          value: 'Option3',
          label: 'Option3',
        },
        {
          value: 'Option4',
          label: 'Option4',
        },
        {
          value: 'Option5',
          label: 'Option5',
        },
      ]
    }
  },
  methods: {
    register() {

      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: "error",
          message: '2次密码输入不一致！'
        })
        return
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.permission=2;
          request.post("/user/register", this.form).then(res => {
            //console.log(res);
            if (res.data.code === 0) {
              this.$message({
                type: "success",
                message: "注册成功"
              })
              this.$router.push("/login")  //登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
#register{
  background: url("../assets/background2.jpeg");
  background-size: 100% 100%;
  width:100%;
  height:100%;
  position:fixed;
}
</style>
